package com.dzjcloud.dzj.common.minio;

import com.dzjcloud.dzj.common.minio.service.MinioTemplate;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;

import java.util.Objects;


/**
 * <p>
 * minio 自动配置类
 * </p>
 *
 * @author 董泽建
 * @Version: V1.0
 * @since 2019-04-13 17:46
 */
@AllArgsConstructor
@EnableConfigurationProperties({MinioProperties.class})
@Order(-1)
public class MinioAutoConfiguration {

    private final MinioProperties properties;

    @Bean
    @ConditionalOnMissingBean(MinioTemplate.class)
    @ConditionalOnProperty(name = "minio.url")
    MinioTemplate template() {
        //如果secure为空设置true
        if (Objects.isNull(properties.getSecure())) {
            properties.setSecure(Boolean.FALSE);
        }
        return new MinioTemplate(
                properties.getUrl(),
                properties.getUsername(),
                properties.getPassword(),
                properties.getSecure()
        );
    }

}
